/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Project2.cs342.project1.mathNotebook.data;


import java.math.*;
import java.util.*;
import java.io.*;
import java.security.*;
import java.security.spec.*;


/**
 *
 * @author bosmak40
 */
public class KeyTextManager {

    


    public KeyTextManager(){


    }
 

    public void writeLocalDesKey(String desKey){
        try{

	    PrintWriter out = new PrintWriter(new FileWriter(new File("localDes.txt")));
	    out.println(desKey);
	    out.close();
            
	}
	catch (Exception ex){
	    System.out.println("Error writing file");
	}

    }


    public void writeDatabaseDesKey(BigInteger desKey){
        try{

	    PrintWriter out = new PrintWriter(new FileWriter(new File("databaseDes.txt")));
	    out.println(desKey.toString());
	    out.close();
            
	}
	catch (Exception ex){
	    System.out.println("Error writing file");
	}

    }


    public void writeRSAPublicKey(byte[] publicKey){
        try{

	    FileOutputStream fos = new FileOutputStream("public.txt");
	    fos.write(publicKey);
	    fos.close();
            
	}
	catch (Exception ex){
	    System.out.println("Error writing file");
	}

    }



    public void writeRSAPrivateKey(byte[] privateKey){
        try{

	    FileOutputStream fos = new FileOutputStream("private.txt");
	    fos.write(privateKey);
	    fos.close();
            
	}
	catch (Exception ex){
	    System.out.println("Error writing file");
	}

    }


    public String readLocalDesKey(){
       String desKey = "";

	try{

	    Scanner scanner = new Scanner(new FileInputStream(new File("localDes.txt")));
	    desKey = scanner.next();
	    scanner.close();
	   
            
	}
	catch (Exception ex){
	    System.out.println("Error writing file");
	}

	return desKey;

    }

    public BigInteger readDatabaseDesKey(){
        String desKey = "";
	BigInteger encryptedDesKey = null;

	try{

	    Scanner scanner = new Scanner(new FileInputStream(new File("localDes.txt")));
	    desKey = scanner.next();
	    scanner.close();

	    encryptedDesKey = new BigInteger(desKey);
	   
            
	}
	catch (Exception ex){
	    System.out.println("Error writing file");
	}

	return encryptedDesKey;

    }

    public byte[] readRSAPublicKey(){
	byte[] publicKey = null;
        try{

	    FileInputStream fin = new FileInputStream("public.txt");
	    publicKey = new byte[fin.available()];
	    fin.read(publicKey);
	    fin.close();
            
	}
	catch (Exception ex){
	    System.out.println("Error writing file");
	}

	return publicKey;

    }


    public byte[] readRSAPrivateKey(){
	byte[] privateKey = null;
        try{

	    FileInputStream fin = new FileInputStream("public.txt");
	    privateKey = new byte[fin.available()];
	    fin.read(privateKey);
	    fin.close();
            
	}
	catch (Exception ex){
	    System.out.println("Error writing file");
	}

	return privateKey;

    }

 
}
